package com.mezz.bones.im.common.process.impl;

import com.mezz.bones.im.common.event.MessageReceivedEvent;
import com.mezz.bones.im.common.event.MessageSendWithConnectEvent;
import com.mezz.bones.im.common.event.MessageSendWithUserIdEvent;
import com.mezz.bones.im.common.process.IMessageProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 层与层消息交互的抽象处理器 可选择的交互方式(1。本地spring事件监听 2。MQ 3.RPC 4.HTTP)
 * 2. 后续可使用MQ异步发布事件 让 im-gateway 跟 im-logic 解耦
 */
@Service
public class MessageProcessorImpl implements IMessageProcessor {

    @Resource
    private ApplicationContext applicationContext;

    /**
     * 接入层接收并解析数据后 发送消息到业务层处理具体的业务逻辑
     * 调用方向 接入层 → 业务逻辑层
     * @param messageReceivedEvent
     */
    @Override
    public void dispatchMessage2Logic(MessageReceivedEvent messageReceivedEvent) {
        applicationContext.publishEvent(messageReceivedEvent);
    }

    /**
     * 业务逻辑处理完成后 发送信息到具体的用户连接
     * 调用方向 业务逻辑层 → 接入层
     * @param messageSendWithConnectEvent
     */

    @Override
    public void sendMessage2UserConnect(MessageSendWithConnectEvent messageSendWithConnectEvent) {
        applicationContext.publishEvent(messageSendWithConnectEvent);
    }

    /**
     * 业务逻辑处理完成后 发送信息到具体的用户(多客户端消息送达)
     * 调用方向 业务逻辑层 → 接入层
     * @param messageSendWithUserIdEvent
     */
    @Override
    public void sendMessage2UserId(MessageSendWithUserIdEvent messageSendWithUserIdEvent) {
        applicationContext.publishEvent(messageSendWithUserIdEvent);
    }
}
